package com.example.testrxjava.fragment.project;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.testrxjava.ArticleDetailsActivity;
import com.example.testrxjava.R;
import com.example.testrxjava.adapter.ProjectInfoAdapter;
import com.example.testrxjava.base.BaseFragment;
import com.example.testrxjava.fragment.code.WxArticleSFragment;
import com.example.testrxjava.util.Action;
import com.example.testrxjava.util.CommUtil;
import com.example.testrxjava.util.ConTract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 项目名称：TestRxjava
 *
 * @Author 郭迎辉
 * Create by 11:29 on 2019/4/22.
 * Describe: 项目列表fragment
 */
public class ProjectInfoFragment extends BaseFragment {

    @BindView(R.id.project_info_Refresh)
    SmartRefreshLayout project_info_Refresh;
    @BindView(R.id.project_info_Recyler)
    RecyclerView project_info_Recyler;

    private static ProjectInfoFragment instance;
    private int cid;
    private  int page=1;
    private List<ProjectInfoBean.DataBean.DatasBean> datasBeans;
    private ProjectInfoAdapter adapter;
    public static ProjectInfoFragment getInstance(int id) {
        Bundle bundle = new Bundle();
        bundle.putInt(ConTract.KEY_ARTICLE_ID, id);
//        if(instance==null){

        instance = new ProjectInfoFragment();
//        }
        instance.setArguments(bundle);

        return instance;
    }
    private void getData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            cid = bundle.getInt(ConTract.KEY_ARTICLE_ID, -1);
            Log.e("cid==",cid+"");
        }
    }
    @Override
    protected void initData() {
        getData();

        project_info_Refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                    page=1;
                    datasBeans.clear();
                    Action.getInstance().getProjectInfoList(page, cid + "", new ProjectInfoSucessCallback() {
                        @Override
                        public void projectInfoSucess(ProjectInfoBean bean) {
                            datasBeans.addAll(bean.getData().getDatas());
                            adapter.notifyDataSetChanged();
                        }

                        @Override
                        public void projectInfoFail(String error) {
                            CommUtil.Toast(getActivity(),error);
                        }
                    });
                    refreshlayout.finishRefresh();

            }
        });

        project_info_Refresh.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                Action.getInstance().getProjectInfoList(page, cid + "", new ProjectInfoSucessCallback() {
                    @Override
                    public void projectInfoSucess(ProjectInfoBean bean) {
                        datasBeans.addAll(bean.getData().getDatas());
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void projectInfoFail(String error) {
                        CommUtil.Toast(getActivity(),error);
                    }
                });
                refreshlayout.finishLoadmore();

            }
        });



        Action.getInstance().getProjectInfoList(page, cid+"", new ProjectInfoSucessCallback() {
            @Override
            public void projectInfoSucess(ProjectInfoBean bean) {
                datasBeans.addAll(bean.getData().getDatas());
                Log.e("databeans==size",datasBeans.size()+"");
                for (int i = 0; i < datasBeans.size(); i++) {
                    Log.e("88888==projectinfo",datasBeans.get(i).getTitle());
                }
                project_info_Recyler.setLayoutManager(new LinearLayoutManager(getActivity()));
                adapter=new ProjectInfoAdapter(R.layout.project_info_item,datasBeans);
                project_info_Recyler.setAdapter(adapter);


                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        ArticleDetailsActivity.startActivityForResultByFragment(getActivity(),
                                ProjectInfoFragment.this,
                                datasBeans.get(position).getLink(),
                                datasBeans.get(position).getTitle(),
                                datasBeans.get(position).getId(),
                                datasBeans.get(position).isCollect(),
                                ConTract.REQUEST_ARTICLE_ACTIVITY);
                    }
                });

            }

            @Override
            public void projectInfoFail(String error) {
                CommUtil.Toast(getActivity(),error);
            }
        });
    }

    @Override
    protected View initView() {
        View view= LayoutInflater.from(getActivity()).inflate(R.layout.project_info_fragment,null);
        datasBeans=new ArrayList<ProjectInfoBean.DataBean.DatasBean>();
        return view;
    }
}
